package net.yunzhanyi.client.service.impl;

import net.yunzhanyi.client.domain.dto.ClientLoginUser;
import net.yunzhanyi.client.domain.vo.AuthorVo;
import net.yunzhanyi.client.service.AuthorService;
import net.yunzhanyi.client.service.CollectionService;
import net.yunzhanyi.client.utils.AuthUtils;
import net.yunzhanyi.common.core.utils.ObjectUtils;
import net.yunzhanyi.generic.domain.model.Author;
import net.yunzhanyi.generic.domain.model.Poetry;
import net.yunzhanyi.generic.mapper.AuthorMapper;
import net.yunzhanyi.generic.mapper.PoetryMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author bestct
 * @date 2022/9/30
 * @type 类
 */
@Service
public class AuthorServiceImpl implements AuthorService {

    @Autowired
    private AuthorMapper authorMapper;

    @Autowired
    CollectionService collectionService;
    @Autowired
    private PoetryMapper poetryMapper;
    @Override
    public AuthorVo searchWebAuthorById(Long authorId) {
        Author author = authorMapper.selectByPrimaryKey(authorId);
        List<Poetry> poetries=poetryMapper.selectByAuthorIdLimit(author.getId(),20);
        AuthorVo authorVo=new AuthorVo();
        BeanUtils.copyProperties(author,authorVo);
        ClientLoginUser loginUser = AuthUtils.getLoginUser();
        long uid = -1;
        if (ObjectUtils.nonNull(loginUser)) {
            uid = loginUser.getUid();
        }
        authorVo.setCollection(collectionService.isCollected(uid, Long.valueOf(author.getId()),2));

        authorVo.setPoetryList(poetries);
        return authorVo;
    }


    @Override
    public String searchAuthorNameById(Long authorId) {
        String s = authorMapper.selectAuthorNameById(authorId);
        return s;
    }
}
